﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SaturniLibrary.Utility
{
   public class StringNormalize
    {
       /// <summary>
       /// 将中文逗号（，）替换成英文逗号
       /// </summary>
       /// <param name="input"></param>
       /// <returns></returns>
       public static string ReviseChineseComma(string input )
       {
           if (string.IsNullOrWhiteSpace(input))
           {
               return "";
           }
           return input.Replace("，", ",");
       }

       public static string NormalizedMulitInput(string input, char split = ',', bool ignoreCase = true)
       {
           if (string.IsNullOrWhiteSpace(input))
           {
               return string.Empty;
           }
           input = ReviseChineseComma(input);
           string[] inputArr = input.Split(split);
           if (inputArr.Length==1)
           {
               return inputArr[0].Trim();
           }
           List<string> inputList = new List<string>();
           foreach (string inputItem in inputArr)
           {
               if (string.IsNullOrWhiteSpace(inputItem))
               {
                   continue;
               }
               string inputItemTrim = inputItem.Trim();
               if (inputList.Exists(x => x.Equals(inputItemTrim, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.CurrentCulture)))
               {
                   continue;
               }
               inputList.Add(inputItemTrim);
           }
           return string.Join(",", inputList);
       }


       public static bool IsTrueSynonym(string input, params string[] customString)
       {
           if (string.IsNullOrWhiteSpace(input))
           {
               return false;
           }
           if ("Y".Equals(input, StringComparison.OrdinalIgnoreCase) ||
                "Yes".Equals(input, StringComparison.OrdinalIgnoreCase) ||
                "True".Equals(input, StringComparison.OrdinalIgnoreCase) ||
                "1".Equals(input) ||
                "OK".Equals(input, StringComparison.OrdinalIgnoreCase) ||
                "On".Equals(input, StringComparison.OrdinalIgnoreCase))
           {
               return true;
           }

           if (customString.Length > 0)
           {
               return customString.Any(s => s.Equals(input, StringComparison.InvariantCultureIgnoreCase));
           }
           return false;
       }

       public static string ClearupIllegalSqlCharacters(string xInput)
       {
           if (string.IsNullOrWhiteSpace(xInput))
           {
               return string.Empty;
           }

           return xInput.Replace("'", "''");
       }
    }
}
